package entities;


import java.sql.*;



public class ConnectionManager 
{
	/**
	 * return the connection to use to connect to the database
	 * @return the connection
	 */
   Connection con;
	public Connection getConnection()
	{
		if(con!=null)
			return con;
		try
		{
			String url = "jdbc:odbc:" + "SQLConnection"; 	// Assuming "DataSource" is your DataSource name

			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

			try
			{            	
				con = DriverManager.getConnection(url); 
				// Assuming your SQL Server's	username is "username"
				// And password is "password"
			}

			catch (SQLException ex)
			{
				
				ex.printStackTrace();
			}
		}
		catch(ClassNotFoundException e)
		{
			System.out.println(e);
		}
		return con;
	}

	public ResultSet executeSelectQuery(String query )
	{
		Statement stmt = null;    
		ResultSet rs=null;
	    try 
	    {
	        stmt=getConnection().createStatement();
	        rs = stmt.executeQuery(query);
	    }
	    catch(Exception e)
	    {
	    	e.printStackTrace();
	    }
	    
		return rs;
	}
	
	public int executeUpdateQuery(String query )
	{
		Statement stmt = null;    
		int numberOfAffectedRows = 0;
	    try 
	    {
	        stmt=getConnection().createStatement();
	        numberOfAffectedRows = stmt.executeUpdate(query);
	    }
	    catch(Exception e)
	    {
	    	e.printStackTrace();
	    }
	    
		return numberOfAffectedRows;
	}
	
}

